package com.talos.ums.service.organizationmanager;

import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.ums.controller.admin.organizationmanager.vo.UmsOrganizationManagerPageReqVO;
import com.talos.ums.controller.admin.organizationmanager.vo.UmsOrganizationManagerSaveReqVO;
import com.talos.ums.dal.dataobject.organizationmanager.UmsOrganizationManagerDO;
import com.talos.ums.dal.mysql.organizationmanager.UmsOrganizationManagerMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static com.talos.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_NOT_FOUND;
import static com.talos.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 组织管理人员关联表 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class UmsOrganizationManagerServiceImpl implements UmsOrganizationManagerService {

    @Resource
    private UmsOrganizationManagerMapper organizationManagerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOrganizationManager(UmsOrganizationManagerSaveReqVO createReqVO) {
        // 插入
        UmsOrganizationManagerDO organizationManager = BeanUtils.toBean(createReqVO, UmsOrganizationManagerDO.class);
        organizationManagerMapper.insert(organizationManager);
        // 返回
        return organizationManager.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrganizationManager(UmsOrganizationManagerSaveReqVO updateReqVO) {
        // 校验存在
        validateOrganizationManagerExists(updateReqVO.getId());
        // 更新
        UmsOrganizationManagerDO updateObj = BeanUtils.toBean(updateReqVO, UmsOrganizationManagerDO.class);
        organizationManagerMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOrganizationManager(Long id) {
        // 校验存在
        validateOrganizationManagerExists(id);
        // 删除
        organizationManagerMapper.deleteById(id);
    }

    private void validateOrganizationManagerExists(Long id) {
        if (organizationManagerMapper.selectById(id) == null) {
            throw exception(DATA_NOT_FOUND);
        }
    }

    @Override
    public UmsOrganizationManagerDO getOrganizationManager(Long id) {
        return organizationManagerMapper.selectById(id);
    }

    @Override
    public PageResult<UmsOrganizationManagerDO> getOrganizationManagerPage(UmsOrganizationManagerPageReqVO pageReqVO) {
        return organizationManagerMapper.selectPage(pageReqVO);
    }

}
